#include "config_manager.h"
#include <ShlObj.h>
#include "../base/debug.h"
#include "../base/error.h"
#include "../base/logging.h"
#include "../base/utils.h"

namespace 
{
	HRESULT GetDir(int csidl, const CString& path_tail, bool create_dir, CString* dir)
	{
		ASSERT1(dir);

		CString path;
		HRESULT hr = GetFolderPath(csidl | CSIDL_FLAG_DONT_VERIFY, &path);
		if (FAILED(hr))
		{
			CORE_LOG(LW, (_T("GetDir failed to find path [%d][0x%08x]"), csidl, hr));
			return hr;
		}

		if (!::PathAppend(CStrBuf(path, MAX_PATH), path_tail))
		{
			CORE_LOG(LW, (_T("GetDir failed to append path][%s][%s]"), path, path_tail));
			return PCAGENT_E_PATH_APPEND_FAILED;
		}
		dir->SetString(path);

		// Try to create the directory. Continue if the directory can't be created.
		if (create_dir)
		{
			hr = CreateDir(path, NULL);
			if (FAILED(hr))
				CORE_LOG(hr, (_T("[GetDir failed to create dir][%s][0x%08x]"), path, hr));
		}
		return S_OK;
	}
}

LLock ConfigManager::lock_;
ConfigManager* ConfigManager::config_manager_ = NULL;

ConfigManager* ConfigManager::Instance()
{
	__mutexScope(lock_);
	if (!config_manager_)
		config_manager_ = new ConfigManager();
	
	return config_manager_;
}

void ConfigManager::DeleteInstance()
{
	delete config_manager_;
	config_manager_ = NULL;
}

ConfigManager::ConfigManager()
{

}

CString ConfigManager::GetUserPCAgentInstallDir() const
{
	return "";
}

CString ConfigManager::GetMachinePCAgentInstallDir() const
{
	return "";
}